import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import router from '@/router'
const _axios = axios.create({
  baseURL: 'http://www.liulongbin.top:3008'
})
_axios.interceptors.request.use(
  config => {
    if (config.url.startsWith('/my')) {
      config.headers.Authorization = store.state.login.token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
_axios.interceptors.response.use(
  res => {
    if (res.data.code === 0) {
      return res.data
    } else {
      Message.error(res.data.message)
      return Promise.reject(res.data.message)
    }
  },
  error => {
    console.log('error', error)
    if (error.response.status === 401) {
      store.commit('login/logout')
      Message.error(error.response.data.message)
      router.push('/login')
    }

    return Promise.reject(error)
  }
)
export default _axios
